# LoginPage.py
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from appium.webdriver.common.appiumby import AppiumBy

class LoginPage:
    def __init__(self, driver):
        self.driver = driver

        # 定位器
        if driver.capabilities.get('platformName')=='Android':
              self.shopcart_button = (AppiumBy.XPATH, "//android.widget.TextView[@text='购物车']")
              self.tologin_button = (AppiumBy.XPATH, "//android.widget.TextView[@text='去登陆>']")
              self.username_input = (AppiumBy.XPATH, "//android.widget.EditText[@index='1']")
              self.password_input = (AppiumBy.XPATH, "//android.widget.EditText[@index='1']")
        elif driver.capabilities.get('platformName')=='ios':
              self.login_button = (AppiumBy.XPATH, "//XCUIElementTypeStaticText[@name='购物车']")
              self.login_button = (AppiumBy.XPATH, "//XCUIElementTypeStaticText[@name='购物车']")
              self.login_button = (AppiumBy.XPATH, "//XCUIElementTypeStaticText[contains(@value, '去登陆>')]")
        elif driver.capabilities.get('platformName')=='other' :
              print("其他平台")




    #初始化：切换webview_context
    def test_switch_to_webview_context(driver):
        # 切换到Webview上下文,不然除了原生的控件可以获取，其他vue组件无法获取
        print("====切换webview_context=======")
        contexts = driver.contexts
        webview_context = None
        for context in contexts:
            if 'WEBVIEW' in context:
                webview_context = context
                break
        if webview_context:
            driver.switch_to.context(webview_context)
            print(f"Switched11111 to {webview_context}")
        # 切换回Native上下文
        # native_context = [context for context in contexts if 'NATIVE_APP' in context][0]
        # driver.switch_to.context(native_context)





    def wait_for_element_visible(self, locator, timeout=10):
        # 显示等待直到元素可见
        element = WebDriverWait(self.driver, timeout).until(EC.visibility_of_element_located(locator))
        return element
    
    def wait_for_all_element_visible(self, locator, timeout=10):
        # 显示等待直到元素可见                               
        element = WebDriverWait(self.driver, timeout).until(EC.visibility_of_all_elements_located(locator))
        return element
    
    def navigateToLogin(self):
        # 导航到登录页面
        shopcart_button = self.wait_for_element_visible(self.shopcart_button)
        shopcart_button.click()
        tologin_button = self.wait_for_element_visible(self.tologin_button)
        tologin_button.click()

    def clear_username_input(self):
        # 清除用户名输入框的内容
        username_input = self.wait_for_all_element_visible(self.username_input)
        # username_input.clear()
        username_input[0].send_keys(Keys.CONTROL + "a") 
        username_input[0].send_keys(Keys.DELETE)  

    def clear_password_input(self):
        # 清除密码输入框的内容
        password_input = self.wait_for_all_element_visible(self.password_input)
        # password_input.clear()
        print("9999999999999999999999999999999999999999999999999999999999999999999:"+str(password_input))
        password_input[1].send_keys(Keys.CONTROL + "a") 
        password_input[1].send_keys(Keys.DELETE)  

    def enter_username(self, username):
        print("====================页面信息：=========================="+str(self.driver.page_source))
        # 输入用户名
        username_input = self.wait_for_all_element_visible(self.username_input)
        username_input[0].send_keys(username)
    def enter_password(self, password):
        # 输入密码
        password_input = self.wait_for_element_visible(self.password_input)
        password_input[1].send_keys(password)

    def click_login_button(self):
        # 点击登录按钮
        login_button = self.wait_for_element_visible(self.login_button)
        login_button.click()

    def click_shopcart_button(self):
        # 点击购物车按钮
        login_button = self.wait_for_element_visible(self.shopcart_button)
        login_button.click()

    def click_tologin_button(self):
        # 点击去登录按钮
        login_button = self.wait_for_element_visible(self.tologin_button)
        login_button.click()    

    def is_user_logged_in(self):
        # 检查用户是否已登录
        # return 'https://www.macrozheng.com/admin/#/home' in self.driver.current_url
        try:
            element = WebDriverWait(self.driver, 10).until(
                EC.presence_of_element_located(self.success_message)
            )
            if element.is_displayed():
                return True
            else:
                return False
        except TimeoutException:
            print("元素规定时间内未出现")
            return False
            
    def get_error_message(self):
        # 获取错误消息
        error_message_element = self.wait_for_element_visible(self.error_message_locator)
        return error_message_element.text
    




# self.success_message = (By.XPATH, "//span[@class='no-redirect' and contains(text(), '仪表盘')]")
# self.username_input = (By.XPATH, "//username_input[@name='username']")
# self.password_input = (By.XPATH, "//password_input[@name='password']")
# self.login_button = (By.XPATH, "//button[@class='el-button el-button--primary']/span[contains(text(), '登录')]")

#   self.shopcart_button =WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((AppiumBy.XPATH.XPATH, "//android.widget.TextView[@text='购物车']"))) 
#   self.shopcart_button =WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((AppiumBy.XPATH.XPATH, "//android.widget.TextView[@text='去登陆>']"))) 
#   self.shopcart_button =WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((AppiumBy.XPATH.XPATH, "//android.widget.EditText[@index=1]"))) 


